eststo clear


if("`c(os)'"=="Windows"){
	local tableSaveDir = "ReviewResponseResults\"
	local plotSaveDir = "ReviewResponseResults\"
	local pythonSaveDir = "PythonScripts\"
}
else{
	local tableSaveDir = "ReviewResponseResults/"
	local plotSaveDir = "ReviewResponseResults/"
	local pythonSaveDir = "PythonScripts/"
}

//Establish Control Variable Sets
//Spec1 controls are the same for both registrations and VMT 
local spec1Controls = "c.normYear#i.stateGroup c.quadYear#i.stateGroup asinhMetroPop asinhNonMetroPop asinhMetroInc asinhNonMetroInc logrealmeangasprice logemployment loglicenseddrivers logrealstategdp logroadmileage"

//Other controls are not the same for both registrations and VMT 
//Registration Controls
local spec2RegControls = "`spec1Controls' i.transactionDataDummy"
local spec3RegControls = "`spec2RegControls' i.regDummyExtSpec3"
local spec4RegControls = "`spec2RegControls' i.regDummyExtSpec4"


di "`spec3RegControls'"

//VMT Controls
local spec2VMTControls = "`spec1Controls'"
local spec3VMTControls = "`spec2VMTControls' i.vmtDummyExtSpec3"
local spec4VMTControls = "`spec2VMTControls' i.vmtDummyExtSpec4"

//Absorb Vars are always time and state fixed effects
local absorbVars = "ib51.stateGroup ib1995.year"

//Establish Cluster Variable
local clusterVar = "stateGroup year"

//Establish primary null
local primaryNull = 1.6
local altNull = 1
local altNull2 = 0.3
local altNull3 = 0
local altNull4 = -2.5

frame create ruralUrban
frame change ruralUrban

if("`c(os)'"=="Windows"){
	local dataFile = "CleanedData\UrbanRuralVMT.csv"
}
else{
	local dataFile = "CleanedData/UrbanRuralVMT.csv"
}

import delimited using "`dataFile'"

frame change default
frlink 1:1 year state, frame(ruralUrban)
frget ruralvmt urbanvmt, from(ruralUrban)
gen logruralvmt = log(ruralvmt)
gen logurbanvmt = log(urbanvmt)

bro state year ruralvmt urbanvmt

//Urban model
//non-normalized models
local treatmentVar = "1.nosafetyind"

local fsVarsSpec2 = "logregistrations i.nosafetyind `spec2RegControls'"
local rfVarsSpec2 = "logurbanvmt i.nosafetyind `spec2VMTControls'"
local modelTitleSpec2 = "Spec2Urban"

local fsOut = "logregistrations"
local rfOut = "logurbanvmt"

local treatControl = "nosafetyind"

local fsControlsSpec2 = "`spec2RegControls'"
local rfControlsSpec2 = "`spec2VMTControls'"


local nullLowSpec2 = -4
local nullDeltaSpec2 = 0.001
local nullHighSpec2 = 10

// runModel `fsVarsSpec2', `rfVarsSpec2', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
runModelFWL `fsOut', `rfOut', `fsControlsSpec2', `rfControlsSpec2', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

//Rural model
preserve
drop if state=="Dist. of Col."

local fsVarsSpec2 = "logregistrations i.nosafetyind `spec2RegControls'"
local rfVarsSpec2 = "logruralvmt i.nosafetyind `spec2VMTControls'"

local rfOut = "logruralvmt"

local modelTitleSpec2 = "Spec2Rural"
local nullLowSpec2 = -10
local nullDeltaSpec2 = .01
local nullHighSpec2 = 50

runModelFWL `fsOut', `rfOut', `fsControlsSpec2', `rfControlsSpec2', `treatControl', `absorbVars', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'
// runModel `fsVarsSpec2', `rfVarsSpec2', `clusterVar', `treatmentVar', `modelTitleSpec2', `nullLowSpec2', `nullDeltaSpec2', `nullHighSpec2', `primaryNull', `altNull', `altNull2', `altNull3', `altNull4'

restore

// python script "`pythonSaveDir'combineIV.py" 
//
//
// constructTable VMTPerCapitaiv; Fleet Travel Distance Per Capita; Spec1PerCapita Spec2PerCapita Spec3PerCapita Spec4PerCapita; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
// constructTable VMTiv; Fleet Travel Distance ; Spec1 Spec2 Spec3 Spec4; `primaryNull'; `altNull'; `altNull2'; `altNull3'; `altNull4'
